package com.ys.ui
{
	import com.ys.util.Const;
	import com.ys.util.GraphicsUtil;
	import com.ys.util.Universal;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import gs.TweenLite;
	import gs.easing.Quad;
	
	public class Chooser extends Sprite
	{
		private var mainLayer:Sprite = new Sprite;
		private var itemLayer:Sprite = new Sprite;
		
		private var selectedString:String = null;
		private var selectedItem:Button;
		
		public function Chooser(w:Number, h:Number):void
		{
			var modal:Sprite = GraphicsUtil.createBorder(w, h, 0xffffff);
			modal.alpha = 0.8;
			addChild( modal );
			addChild(mainLayer);
			visible = false;
			
			//var btn:Sprite = GraphicsUtil.drawCircularButton(Const.BUTTON_SIZE, Const.DEFAULT_COLOR);
			
			//mainLayer.addChild(btn);
			
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		public function summon(target:DisplayObject, data:Array, size:Number=25):void
		{
			visible = true;
			Universal.removeAllChildren(mainLayer);
			Universal.removeAllChildren(itemLayer);
			mainLayer.filters = [Const.DEFAULT_FILTER];
			mainLayer.addChild(itemLayer);
			
			selectedItem = target as Button;
			
			var r:Number = (Math.PI*2)/data.length;
			var rOffeset:Number = Math.PI/2;
			var dist:Number = 30 + size/2;
			var opp:Number;
			var adj:Number;
			var btn:Button;
			
			var rect:Rectangle = getBounds(target);
			var x:Number = -rect.left;
			var y:Number = -rect.top;
			
			mainLayer.x = x;
			mainLayer.y = y;
			
			var line:Sprite = new Sprite;
			line.graphics.lineStyle(1, Const.DEFAULT_COLOR);
			itemLayer.addChild(line);
			
			var dot:Sprite = new Sprite;
			dot.graphics.beginFill(Const.PALLET_COLOR);
			dot.graphics.drawCircle(0,0, target.width/2-2);
			mainLayer.addChild(dot);
			
			for (var i:int = 0; i<data.length; i++)
			{
				adj = Math.cos(r*i-rOffeset)*dist;
				opp = Math.sin(r*i-rOffeset)*dist;
				
				btn = new Button(size, Const.DEFAULT_COLOR, data[i]);
				btn.x = adj;
				btn.y = opp;
				itemLayer.addChild(btn);
				buttonAddEvents(btn);
				
				adj = Math.cos(r*i-rOffeset)*(dist-size/2);
				opp = Math.sin(r*i-rOffeset)*(dist-size/2);
				
				line.graphics.moveTo(0, 0)
				line.graphics.lineTo(adj, opp);
				
				itemLayer.scaleX = itemLayer.scaleY = 0;
				itemLayer.rotation = 180;
				
				TweenLite.to(itemLayer, Const.DUR_SUMMON, {ease:Quad.easeOut, rotation:0, scaleX:1, scaleY:1})
			}
		}
		
		
		private function buttonAddEvents(btn:Button):void
		{
			btn.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			btn.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			
			function onRollOver(e:MouseEvent):void
			{
				selectedString = btn.textField.text;
			}
			
			function onRollOut(e:MouseEvent):void
			{
				selectedString = null;
			}
		}
		
		private function onMouseUp(e:MouseEvent):void
		{
			hide();
			if (selectedString != null)
			{
				selectedItem.textField.text = selectedString;
				selectedItem.textField.centerPosition();
			}
		}
		
		
		public function hide():void
		{
			visible = false;
		}

	}
}